<?php

class Mcustomer extends CI_Model {
	
	public $table = 'customers';
	public $pri_key="id";


	function __construct()
	{
		parent::__construct();
		load_lang("admin");
	}
	function count_member_online()
	{
		$this->db->where("user_data like '%cus_id%'");
		$query=$this->db->get("ci_sessions");
		 return $query->num_rows(); 
	}
	function count_online()
	{
		
		$query=$this->db->get("ci_sessions");
		return $query->num_rows(); 
	}
	public function insert($data){

		if($this->db->insert($this->table,$data))
			return true;
		
		return false;

	}
	public function update_pass_by_email($data,$email)
	{
		
		$email= trim($email);
		$where= "email = '".$email."'";
		if($this->db->update($this->table,$data,$where))
				return true;
				//print_r($this->db->where());
		else
			return false;
	}
	public function update($data,$id){
	
		$where = $this->pri_key." =".$id;
		if($this->db->update($this->table,$data,$where))
			return true;
		
		return false;

	}
public function check_user($email,$password)
	{
		$where=array("email"=>$email,"password"=>pass_md5($password));
		
		$row=$this->db->get_where($this->table,$where)->result_array();
		
	
		if($this->db->count_all_results() == 1)
		{
			return $row; 
		}
		return false;
	
	}
	public function info_form_id($id)
	{
		if(is_numeric($id) && $id !="")
		{
			$where =$this->pri_key.' ='.$id;
		//echo $where;
		$this->db->where($where);
		
	//	echo $id;
		return $this->db->get($this->table)->result_array();
      //  echo		$this->db->get($this->table)->num_rows();
		//print_r($this->db->get($this->table)->result_array());
		}
		else
			return false;
	}
	public function check_email($email,$id="")
	{
		$this->db->where('email =',$email);
		if($id != "")
		{
			$this->db->where('id != ',$id);
		}
		$query = $this->db->get($this->table);
		//echo $query->num_rows();
		if( $query->num_rows()  >0)
			return true; // email nay da ton tai va ko su dung duoc
		return false; // co the su dung email nay
			
 	}
	public function list_data($num,$off)
	{
		$this->db->limit($off,$num);
		$this->db->order_by($this->pri_key,"DESC");
		if($this->db->count_all($this->table) >0)
			//return $this->db->get($this->table)->result_array();
			{
		
	
	return $this->db->get($this->table)->result_array();
			}
		return false;
	}
	// dem so record 
	function count_all()
	{
		return $this->db->count_all_results($this->table);
	}
	
	




  function resource_search()

	{

		$keyword = htmlentities($this->input->post('keyword'), ENT_QUOTES, 'UTF-8');

    $type = $this->input->post('search_type');

		$sql = "SELECT id, avatar, date_join,

                   last_name, first_name, full_name, phone, address,

                   active, email, last_login

            FROM human_resources WHERE ";

    if ($type == 'id')

      $sql .= "id = '$keyword' ";

    elseif ($type == 'first_name')

      $sql .= "first_name LIKE '%$keyword%' ";

    elseif ($type == 'full_name')

      $sql .= "full_name LIKE '%$keyword%' ";



    $sql .= "ORDER BY id DESC";



		$Q = $this->db->query($sql);

		if ($Q->num_rows() > 0)

		{

			return $Q->result_array();

		}



		return false;

	}



	



	function delete()

	{

		if (isset($_POST['select_id']))

		{

			foreach($_POST['select_id'] as $id) {

			//	$row = $this->info_from_id($id);

		

				//xoa file

		//		if ($row['avatar'] != 'null') unlink($row['avatar']);

		

				//xoa CSDL

			//	$sql = "DELETE FROM human_resources WHERE id=$id";
				$this->db->where($this->pri_key, $id);
				$this->db->delete($this->table); 
				//$this->db->query($sql);

			}			

		}

	}



  /*===================BAN ĐƯƠNG NHIỆM===================*/

  function bdn_review($begin, $limit) //DS Ban đương nhiệm

	{

		$sql = "SELECT dn.id, hr.active,

                   hr.full_name, hr.email, hr.phone,

                   dn.note

            FROM human_resources hr

            INNER JOIN human_duong_nhiem dn

            ON dn.id_human_resource = hr.id

            ORDER BY hr.id DESC

            LIMIT $begin,$limit

		";

		$Q = $this->db->query($sql);

		if ($Q->num_rows() > 0)

		{

			return $Q->result_array();

		}



		return false;

	}



  function bdn_create()

	{

		//missing

		$expected = array(

      'id_human_resource',

      'note'

    );

		foreach ($expected as $value)

		{

			if (array_key_exists($value, $_POST))

      {

        ${$value} = $_POST[$value];

      }

      else

      {

        ${$value} = 'null';

      }

    }



		//create

    $sql = "INSERT INTO human_duong_nhiem SET

       id_human_resource = '$id_human_resource',

       note = '$note'

    ";

		if($this->db->query($sql)) return true;

    else return false;

	}



  function bdn_update($id) //return true/false

	{

		//missing

		$expected = array(

      'id_human_resource',

      'note'

    );



		foreach ($expected as $value)

		{

			if (array_key_exists($value, $_POST))

      {

        ${$value} = $_POST[$value];

      }

      else

      {

        ${$value} = 'null';

      }

		}



    //update

     $sql = "UPDATE human_duong_nhiem SET

                    id_human_resource = '$id_human_resource',

                    note = '$note'

             WHERE id=$id

    ";



		if($this->db->query($sql)) return true;

    else return false;

	}



  function bdn_delete()

	{

		if (isset($_POST['select_id']))

		{

			foreach($_POST['select_id'] as $id) {

				//xoa CSDL

				$sql = "DELETE FROM human_duong_nhiem WHERE id=$id";

				$this->db->query($sql);

			}

		}

	}



  function bdn_search()

	{

		$keyword = htmlentities($this->input->post('keyword'), ENT_QUOTES, 'UTF-8');

    $type = $this->input->post('search_type');

		$sql = "SELECT dn.id, hr.avatar, hr.date_join,

                   hr.last_name, hr.first_name, hr.full_name, hr.phone, hr.address,

                   hr.active, hr.email, hr.last_login

            FROM human_resources hr, human_duong_nhiem dn

            WHERE dn.id_human_resource = hr.id AND ";

    if ($type == 'id')

      $sql .= "hr.id = '$keyword' ";

    elseif ($type == 'first_name')

      $sql .= "first_name LIKE '%$keyword%' ";

    elseif ($type == 'full_name')

      $sql .= "full_name LIKE '%$keyword%' ";



    $sql .= "ORDER BY dn.id DESC";



		$Q = $this->db->query($sql);

		if ($Q->num_rows() > 0)

		{

			return $Q->result_array();

		}



		return false;

	}



  function info_from_id_bdn($id)

	{

		$sql = "SELECT id, id_human_resource, note

            FROM human_duong_nhiem

            WHERE id=$id";

		$Q = $this->db->query($sql);

		if ($Q->num_rows() > 0)

		{

			return $Q->row_array();

		}



		return false;

	}



  /*===================DS GIỮ TÀI SẢN===================*/

  function taisan_review($begin, $limit)

	{

		$sql = "SELECT ts.id, hr.active,

                   hr.full_name, hr.email, hr.phone,

                   ts.note

            FROM human_resources hr

            INNER JOIN human_tai_san ts

            ON ts.id_human_resource = hr.id

            ORDER BY hr.id DESC

            LIMIT $begin,$limit

		";

		$Q = $this->db->query($sql);

		if ($Q->num_rows() > 0)

		{

			return $Q->result_array();

		}



		return false;

	}



  function taisan_create()

	{

		//missing

		$expected = array(

      'id_human_resource',

      'note'

    );

		foreach ($expected as $value)

		{

			if (array_key_exists($value, $_POST))

      {

        ${$value} = $_POST[$value];

      }

      else

      {

        ${$value} = 'null';

      }

    }



		//create

    $sql = "INSERT INTO human_tai_san SET

       id_human_resource = '$id_human_resource',

       note = '$note'

    ";

		if($this->db->query($sql)) return true;

    else return false;

	}



  function taisan_update($id) //return true/false

	{

		//missing

		$expected = array(

      'id_human_resource',

      'note'

    );



		foreach ($expected as $value)

		{

			if (array_key_exists($value, $_POST))

      {

        ${$value} = $_POST[$value];

      }

      else

      {

        ${$value} = 'null';

      }

		}



    //update

     $sql = "UPDATE human_tai_san SET

                    id_human_resource = '$id_human_resource',

                    note = '$note'

             WHERE id=$id

    ";



		if($this->db->query($sql)) return true;

    else return false;

	}



  function taisan_delete()

	{

		if (isset($_POST['select_id']))

		{

			foreach($_POST['select_id'] as $id) {

				//xoa CSDL

				$sql = "DELETE FROM human_tai_san WHERE id=$id";

				$this->db->query($sql);

			}

		}

	}



  function taisan_search()

	{

		$keyword = htmlentities($this->input->post('keyword'), ENT_QUOTES, 'UTF-8');

    $type = $this->input->post('search_type');

		$sql = "SELECT ts.id, hr.avatar, hr.date_join,

                   hr.last_name, hr.first_name, hr.full_name, hr.phone, hr.address,

                   hr.active, hr.email, hr.last_login

            FROM human_resources hr, human_tai_san ts

            WHERE ts.id_human_resource = hr.id AND ";

    if ($type == 'id')

      $sql .= "hr.id = '$keyword' ";

    elseif ($type == 'first_name')

      $sql .= "first_name LIKE '%$keyword%' ";

    elseif ($type == 'full_name')

      $sql .= "full_name LIKE '%$keyword%' ";



    $sql .= "ORDER BY ts.id DESC";



		$Q = $this->db->query($sql);

		if ($Q->num_rows() > 0)

		{

			return $Q->result_array();

		}



		return false;

	}



  function info_from_id_taisan($id)

	{

		$sql = "SELECT id, id_human_resource, note

            FROM human_tai_san

            WHERE id=$id";

		$Q = $this->db->query($sql);

		if ($Q->num_rows() > 0)

		{

			return $Q->row_array();

		}



		return false;

	}



  /*===================DS THAM GIA SỰ KIỆN===================*/

  function event_review($id_event, $begin, $limit, $active='')

	{

		$sql = "SELECT he.id, hr.id AS id_user, hr.active,

                   hr.full_name, hr.email, hr.phone

            FROM human_resources hr, human_event he, events

            WHERE he.id_event = $id_event

                  AND he.id_human_resource = hr.id

                  AND he.id_event = events.id

            ORDER BY he.id DESC

            LIMIT $begin,$limit

		";

		$Q = $this->db->query($sql);

		if ($Q->num_rows() > 0)

		{

			return $Q->result_array();

		}



		return false;

	}



  function event_create()

	{

		//missing

		$expected = array(

      'id_event',

      'id_human_resource'

    );

		foreach ($expected as $value)

		{

			if (array_key_exists($value, $_POST))

      {

        ${$value} = $_POST[$value];

      }

      else

      {

        ${$value} = 'null';

      }

    }

  

    //kiem tra chuoi id_human_resource la mot chuoi ngan cach nhau boi dau ';'

    $a_id = explode(';', $id_human_resource);

    foreach ($a_id as $value) //chuỗi id_human_resource hợp lệ;

    {

      $value = trim($value);

      $sql = "INSERT INTO human_event SET

        id_event = '$id_event',

        id_human_resource = '$value'

      ";

      if (!$this->db->query($sql)) return false;

    }



    return true;

	}



  function event_delete()

	{

		if (isset($_POST['select_id']))

		{

			foreach($_POST['select_id'] as $id) {

				//xoa CSDL

				$sql = "DELETE FROM human_event WHERE id = '$id'";

				$this->db->query($sql);

			}

		}

	}



  function event_search()

	{

		$keyword = htmlentities($this->input->post('keyword'), ENT_QUOTES, 'UTF-8');

    $type = $this->input->post('search_type');

		$sql = "SELECT ev.id, hr.avatar, hr.date_join,

                   hr.last_name, hr.first_name, hr.full_name, hr.phone, hr.address,

                   hr.active, hr.email, hr.last_login

            FROM human_resources hr, human_event ev

            WHERE ev.id_human_resource = hr.id AND ";

    if ($type == 'id')

      $sql .= "hr.id = '$keyword' ";

    elseif ($type == 'first_name')

      $sql .= "first_name LIKE '%$keyword%' ";

    elseif ($type == 'full_name')

      $sql .= "full_name LIKE '%$keyword%' ";



    $sql .= "ORDER BY ev.id DESC";



		$Q = $this->db->query($sql);

		if ($Q->num_rows() > 0)

		{

			return $Q->result_array();

		}



		return false;

	}



  function info_from_id_event($id)

	{

		$sql = "SELECT id, id_event, id_human_resource

            FROM human_event

            WHERE id=$id";

		$Q = $this->db->query($sql);

		if ($Q->num_rows() > 0)

		{

			return $Q->row_array();

		}



		return false;

	}



  function check_id_event($id_event)

  {

    if (is_numeric($id_event))

    {

      $sql = "SELECT id

              FROM events

              WHERE id = $id_event

      ";

      $Q = $this->db->query($sql);

      if ($Q->num_rows() == 1)

      {

        return true;

      }

    }



		return false;

  }

	/*=================== OTHER METHODS ===================*/

  function check_id_exist($id_human_resource)

  {

    $sql = "SELECT id

            FROM human_resources

            WHERE id = $id_human_resource

		";

		$Q = $this->db->query($sql);

		if ($Q->num_rows() == 1)

		{

			return true;

		}



		return false;

  }

	function total_row()

	{

		$sql = "SELECT id FROM human_resources";

		$Q = $this->db->query($sql);

		return $Q->num_rows();

	}



  function total_row_bdn()

	{

		$sql = "SELECT id FROM human_duong_nhiem";

		$Q = $this->db->query($sql);

		return $Q->num_rows();

	}



  function total_row_taisan()

	{

		$sql = "SELECT id FROM human_tai_san";

		$Q = $this->db->query($sql);

		return $Q->num_rows();

	}



  function total_row_event($id_event='')

	{

    if (empty($id_event))

    {

      $sql = "SELECT id FROM human_event";

    }

    else

    {

      $sql = "SELECT id FROM human_event WHERE id_event=$id_event";

    }

		$Q = $this->db->query($sql);

		return $Q->num_rows();

	}

	

	function info_from_id($id)

	{

		$sql = "SELECT id, avatar, date_join,

                   last_name, first_name, full_name, phone, address,

                   active, email, password, last_login

            FROM human_resources

            WHERE id=$id";

		$Q = $this->db->query($sql);

		if ($Q->num_rows() > 0)

		{

			$row = $Q->row_array();

      //clear text  

      $row['last_name'] = html_entity_decode($row['last_name'], ENT_QUOTES, 'UTF-8');

      $row['first_name'] = html_entity_decode($row['first_name'], ENT_QUOTES, 'UTF-8');

      $row['full_name'] = html_entity_decode($row['full_name'], ENT_QUOTES, 'UTF-8');

      $row['address'] = html_entity_decode($row['address'], ENT_QUOTES, 'UTF-8');

			return $row;

		}

		

		return false;

	}



  //duplicate

	function duplicate($id_begin, $nums) //$nums la so phien ban

	{

		$row = $this->info_from_id($id_begin);

		for($i=1; $i<=$nums ; $i++)

		{

			$new_name = htmlentities($row['full_name'].' - '.$i, ENT_QUOTES, 'UTF-8');

      

			//create

			$sql = "INSERT INTO human_resources SET

                avatar = '".$row['avatar']."',

                active = '".$row['active']."',

                last_name = '".$row['last_name']."',

                first_name = '".$row['first_name']."',

                full_name = '$new_name',

                date_join = '".$row['date_join']."',

                phone = '".$row['phone']."',

                address = '".$row['address']."',

                email = '".$row['email']."',

                password = '".$row['password']."',

                last_login = '".$row['last_login']."'

			";

			if (! $this->db->query($sql))

				return false;

		}

		return true;

	}

}//end model class





/* End of file mhumans.php */

/* Location: ./system/application/models/mhumans.php */

?>